<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-input v-model="dataForm.keyword.key" placeholder=" 请输入用户手机号/订单编号/商品名称查询" clearable style="width: 300px;"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <!-- <el-button v-if="isAuth('picbook:pborder:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <!-- <el-button v-if="isAuth('picbook:pborder:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
      <br>
      <el-form-item label="商品来源">
        <el-select v-model="dataForm.keyword.mediaSource" style="width: 150px;" @change="getDataList(1)">
          <el-option
            v-for="item in sourceOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单分类">
        <el-select v-model="dataForm.keyword.orderType" style="width: 150px;" @change="getDataList(1)">
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式">
        <el-select v-model="dataForm.keyword.payMethod" style="width: 150px;" @change="getDataList(1)">
          <el-option
            v-for="item in payOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态">
        <el-select v-model="dataForm.keyword.payStatus" style="width: 150px;" @change="getDataList(1)">
          <el-option
            v-for="item in statusOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="downloadHandle()">导出订单</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="orderNo"
        header-align="center"
        align="center"
        label="订单编号">
      </el-table-column>
      <el-table-column
        prop="userPhone"
        header-align="center"
        align="center"
        label="用户手机号">
      </el-table-column>
      <el-table-column
        prop="orderTypeName"
        header-align="center"
        align="center"
        label="订单分类">
      </el-table-column>
      <el-table-column
        prop="orderName"
        header-align="center"
        align="center"
        label="订单名称">
      </el-table-column>
      <el-table-column
        prop="price"
        header-align="center"
        align="center"
        label="订单价格(元)">
      </el-table-column>
      <el-table-column
        prop="finalprice"
        header-align="center"
        align="center"
        label="支付价格(元)">
      </el-table-column>
      <el-table-column
        prop="payTime"
        header-align="center"
        align="center"
        label="订单时间">
      </el-table-column>
      <el-table-column
        prop="payMethodName"
        header-align="center"
        align="center"
        label="支付方式">
      </el-table-column>
      <!-- 0 未支付， 1 已支付， 2 取消 -->
      <el-table-column
        prop="payStatusName"
        header-align="center"
        align="center"
        label="支付状态">
      </el-table-column>
      <el-table-column
        prop="mediaSourceName"
        header-align="center"
        align="center"
        label="商品来源">
      </el-table-column>
      <el-table-column
        prop=""
        header-align="center"
        align="center"
        label="分成比例">
      </el-table-column>
      <!-- <el-table-column
        prop="orderStatus"
        header-align="center"
        align="center"
        label="0下单成功，1下单失败，2确认成功，3确认失败">
      </el-table-column> -->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="detailHandle(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './pborder-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          keyword: {
            key: '',
            mediaSource: '',
            orderType: '',
            payMethod: '',
            payStatus: ''
          }
        },
        sourceOptions: [],
        typeOptions: [],
        payOptions: [],
        statusOptions: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
      this.getSource()
      this.getType()
      this.getPayMethod()
      this.getStatus()
    },
    methods: {
      // 获取数据列表
      getDataList (formSearch) {
        // 搜索框来源
        if (formSearch === 1) {
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/picbook/pborder/list'),
          method: 'post',
          data: this.$http.adornData({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keyword': this.dataForm.keyword
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 获取商品来源
      getSource () {
        this.$http({
          url: this.$http.adornUrl('/picbook/combobox/page/listMediaSource'),
          method: 'post'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.sourceOptions = data.data
          } else {
            this.sourceOptions = []
          }
        })
      },
      // 获取订单分类
      getType () {
        this.$http({
          url: this.$http.adornUrl('/picbook/combobox/page/listOrderType'),
          method: 'post'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.typeOptions = data.data
          } else {
            this.typeOptions = []
          }
        })
      },
      // 获取支付方式
      getPayMethod () {
        this.$http({
          url: this.$http.adornUrl('/picbook/combobox/page/listPayMethod'),
          method: 'post'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.payOptions = data.data
          } else {
            this.payOptions = []
          }
        })
      },
      // 获取支付方式
      getStatus () {
        this.$http({
          url: this.$http.adornUrl('/picbook/combobox/page/listPayStatus'),
          method: 'post'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.statusOptions = data.data
          } else {
            this.statusOptions = []
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 详情
      detailHandle (dataForm) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(dataForm)
        })
      },
      downloadHandle () {
        // window.location = this.$http({
        //   url: this.$http.adornUrl('/picbook/pborder/excelExport'),
        //   method: 'post',
        //   data: this.$http.adornData({
        //     'page': this.pageIndex,
        //     'limit': this.pageSize,
        //     'keyword': this.dataForm.keyword
        //   })
        // })

        this.$http({
          url: this.$http.adornUrl('/picbook/pborder/excelExport'),
          method: 'post',
          data: this.$http.adornData({
            'keyword': this.dataForm.keyword
          }),
          responseType:'blob'  //指定blob类型
        }).then(res => {
          const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
          const downloadElement = document.createElement('a');
          const href = window.URL.createObjectURL(blob);
          downloadElement.href = href;
          downloadElement.download = '订单'+new Date().getTime()+'.xls';  //文件名（自己随意设置）
          document.body.appendChild(downloadElement);
          downloadElement.click();
          document.body.removeChild(downloadElement) ;// 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
          this.dataListLoading = false
        })
      }
    }
  }
</script>
